import greenfoot.*;

/**
 * This class defines a crab. Crabs live on the beach.
 */
public class Crab extends Actor
{
    private GreenfootImage image1=null;
    private GreenfootImage image2=null;
    
    public Crab(){
        image1 = new GreenfootImage("crab.png");
        image2 = new GreenfootImage("crab2.png");
        setImage(image1);
    }
    int mark=0;
    public void act()
    {
        move(5);
        turnAtEdge();
        control();
        animation();
        if(isTouching(Worm.class)){
            Greenfoot.playSound("slurp.wav");
            removeTouching(Worm.class);
            mark++;
        }
        if(mark==5){
            Greenfoot.playSound("fanfare.wav");
        }
        
    }
   
    
    public void turnAtEdge()
    {
        if(isAtEdge())
        {
            turn(20);
        }
    }
    
    public void control()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            turn(-15);
        }
        if(Greenfoot.isKeyDown("right"))
        {
            turn(15);
        }
    }
    
    public void animation(){
        if(getImage() == image1){
            setImage(image2);
        }else{
            setImage(image1);
        }
    }
}


